package services

import (
	"golang.org/x/net/context"
	"mall_frontend_api/biz/dal"
	"mall_frontend_api/biz/handler/base"
	"mall_frontend_api/biz/lib/biz_error"
	"mall_frontend_api/biz/model"
	"mall_frontend_api/biz/model/user"
	"mall_frontend_api/biz/pack"
	"mall_frontend_api/biz/util"
)

type UserService struct {
	common *base.Response
	ctx    context.Context
	dao    *dal.InitDAO
}

func NewUserService(ctx context.Context, c *base.Response) *UserService {
	return &UserService{
		common: c,
		ctx:    ctx,
		dao:    dal.NewInitDAO(),
	}
}

func (u *UserService) Register(req *user.RegisterRequest) error {
	data := &model.User{
		FirstName: req.GetFirstName(),
		LastName:  req.GetLastName(),
		Account:   req.Email,
		BizID:     u.common.GetBizID(),
		Password:  util.Md5(req.GetPassword(), true),
		Email:     req.Email,
		CreatedBy: pack.GetUserName(u.common.GetUserInfo()),
	}
	if _, err := u.dao.UserDAO.Create(u.ctx, data); err != nil {
		return biz_error.NewBizError().SetLogMessage("创建user 失败 data %s", util.GetJsonStyle(data)).SetServerErrorMessage("create user failed")
	}
	return nil
}
